const QQMapWX = require('../../assets/js/qqmap-wx-jssdk')
const qqmapsdk = new QQMapWX({
  key: 'DERBZ-EQJ6J-U6JFN-X2DVO-UMC2Z-HQFNQ'
})
const citySelector = requirePlugin('citySelector');
const chooseLocation = requirePlugin('chooseLocation');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    words: ['公交车站', '餐饮', '医院', '学校', '景点', '休闲', '娱乐', '电影'],
    word: '方特世界',
    address: '株洲科泰',
    markers: {},
    latitude: 0,
    longitude: 0,
    windowHeight: '',
  },
  findcity() {
    const key = 'DERBZ-EQJ6J-U6JFN-X2DVO-UMC2Z-HQFNQ'; // 使用在腾讯位置服务申请的key
    const referer = 'wenxcx'; // 调用插件的app的名称
    const hotCitys = '北京,上海,广州,深圳'; // 用户自定义的的热门城市
    wx.navigateTo({
      url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}`,
    })
  },
  formSubmit() {
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: _this.data.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function (res: any) {//成功后的回调
        var res = res.result;
        let location = res.location.lat + ',' + res.location.lng
        _this.setData({
          latitude: res.location.lat,
          longitude: res.location.lng,
        })
        _this.nearby_search(location)
      },
      fail: function (error: any) {
        console.error(error);
      },
      complete: function (res: any) {
        console.log(res);
      }
    })
  },
  nearby_search: function (location: any) {
    var _this = this;
    // 调用接口
    qqmapsdk.search({
      keyword: _this.data.word,  //搜索关键词
      location: location,  //设置周边搜索中心点
      success: function (res: any) { //搜索成功后的回调
        var mks = []
        for (var i = 0; i < res.data.length; i++) {
          mks.push({ // 获取返回结果，放到mks数组中
            title: res.data[i].title,
            id: res.data[i].id,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng,
            iconPath: "../../img/mk.png", //图标路径
            width: 20,
            height: 20,
            _distance: res.data[i]._distance,
            callout: {
              content: res.data[i].title + "\n" + "距离您" + res.data[i]._distance + "m",
              color: '#404040',
              bgColor: '#ffffff',
              borderWidth: 1,
              borderColor: '#8a8a8a',
              fontSize: 14,
              padding: 10,
              borderRadius: 10,
              display: 'ALWAYS'
            }
          })
        }
        _this.setData({
          markers: mks
        });
        console.log(_this.data.markers);

      },
      fail: function (res: any) {
        console.log(res);
      },
      complete: function (res: any) {
        console.log(res);
      }
    });
  },
  onwords(e: any) {
    this.setData({
      word: this.data.words[e.currentTarget.dataset.i]
    });
    this.nearby_search(this.data.latitude + "," + this.data.longitude)
  },
  selectcity() {
    let _this = this;
    const key = 'DERBZ-EQJ6J-U6JFN-X2DVO-UMC2Z-HQFNQ'; //使用在腾讯位置服务申请的key
    const referer = 'wenxcx'; //调用插件的app的名称
    const location = JSON.stringify({
      latitude: _this.data.latitude,
      longitude: _this.data.longitude,
      word: _this.data.address
    });
    const category = '生活服务,娱乐休闲';
    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
    });

  },
  go() {
    let _this = this;
    const key = 'DERBZ-EQJ6J-U6JFN-X2DVO-UMC2Z-HQFNQ'; //使用在腾讯位置服务申请的key
    const referer = 'wenxcx'; //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      name: _this.data.address,
      latitude: _this.data.latitude,
      longitude: _this.data.longitude
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options: any) {
    let show = true;
    let _this = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        show = false;
        _this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          word: '北大青鸟'
        })
        let location = res.latitude + ',' + res.longitude;
        _this.nearby_search(location);
      }
    });
    if (show)
      this.formSubmit()
    var windowHeight = wx.getSystemInfoSync().windowHeight
    this.setData({
      windowHeight: (windowHeight - 44) + 'px'
    })
  },
  onShow() {
    const selectedCity = citySelector.getCity(); // 选择城市后返回城市信息对象，若未选择返回null
    const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
    console.log(location);
    console.log(selectedCity);
    if (selectedCity != null) {
      this.setData({
        address: selectedCity.name
      });
    }
    if (location != null) {
      this.setData({
        address: location.name
      });
    }
    this.formSubmit();
  },
  onUnload() {
    // 页面卸载时清空插件数据，防止再次进入页面，getCity返回的是上次的结果
    citySelector.clearCity();
    chooseLocation.clearCity();
  }
})
